﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Mozlite.Components;

namespace Mozlite.WebUI.Mvc4.Backend
{
    [AuthorizeEx]
    public abstract class BaseController : Mozlite.WebUI.Mvc4.BaseController
    {
        private BackendSettings bSettings;
        /// <summary>
        /// 获取当前后台设置。
        /// </summary>
        public BackendSettings BSettings
        {
            get
            {
                if (bSettings == null)
                    bSettings = BackendSettings.Current;
                return bSettings;
            }
        }

        /// <summary>
        /// 获取当前语言资源字符串。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <returns>返回资源字符串。</returns>
        public override string Local(object key)
        {
            var resource = Cores.GetGlobalResourceByKey(key, AreaName);
            if (resource == null)
                resource = base.Local(key);
            return resource;
        }

        /// <summary>
        /// 获取当前语言资源字符串。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <param name="args">格式化参数。</param>
        /// <returns>返回格式化后的资源字符串。</returns>
        public override string Local(object key, params object[] args)
        {
            var resource = Cores.GetGlobalResourceByKey(key, AreaName);
            if (resource == null)
                resource = base.Local(key, args);
            else
                resource = resource.FormatWith(args);
            return resource;
        }

        private string areaName;
        /// <summary>
        /// 当前区域名称。
        /// </summary>
        public string AreaName
        {
            get
            {
                if (areaName == null)
                    areaName = ControllerContext.GetAreaName();
                return areaName;
            }
        }

        /// <summary>
        /// 获取排序规则。
        /// </summary>
        /// <param name="defaultValue">默认排序。</param>
        /// <returns>返回KendoGrid排序规则。</returns>
        protected override string OrderBy(string defaultValue)
        {
            var field = Request["sort[0][field]"];
            if (field.HasValue())
                return "{0} {1}".FormatWith(field.Trim(), Request.Get<string>("sort[0][dir]", "DESC"));
            return base.OrderBy(defaultValue);
        }

        /// <summary>
        /// 附加资源JSON，每个附加的节点必须以“,”开头。
        /// </summary>
        /// <param name="sb">JSON存储对象。</param>
        protected override void AppendResourceJSON(StringBuilder sb)
        {
            sb.Append(",pager:{");
            sb.AppendFormat("first:'{0}', last:'{1}', previous:'{2}', next:'{3}', display:'{4}', empty:'{5}', page:'{6}', itemsPerPage:'{7}'", Local("Pager_FirstText"), Local("Pager_LastText"), Local("Pager_PreviousText"), Local("Pager_NextText"), Local("Pager_DisplayText"), Local("Pager_EmptyText"), Local("Pager_PageText"), Local("Pager_ItemsPerPageText"));
            sb.Append("}");
        }
        /// <summary>
        /// 显示JSON错误信息。
        /// </summary>
        /// <param name="message">错误信息。</param>
        /// <returns>返回Json试图结果。</returns>
        protected virtual JsonResult JsonError(string message)
        {
            return Json(new { error = message });
        }
        /// <summary>
        /// 显示JSON成功信息。
        /// </summary>
        /// <param name="message">成功信息。</param>
        /// <returns>返回Json试图结果。</returns>
        protected virtual JsonResult JsonSuccess(string message)
        {
            return Json(new { message = message });
        }
        /// <summary>
        /// 显示JSON错误信息。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <param name="args">格式化参数。</param>
        /// <returns>返回Json试图结果。</returns>
        protected virtual JsonResult JsonLocalError(object key, params object[] args)
        {
            return Json(new { error = Local(key, args) });
        }
        /// <summary>
        /// 显示JSON成功信息。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <param name="args">格式化参数。</param>
        /// <returns>返回Json试图结果。</returns>
        protected virtual JsonResult JsonLocalSuccess(object key, params object[] args)
        {
            return Json(new { message = Local(key, args) });
        }
        /// <summary>
        /// 判断验证的结果。
        /// </summary>
        /// <param name="message">错误信息。</param>
        /// <returns>返回Json试图结果。</returns>
        protected virtual JsonResult JsonCheckResult(string message = null)
        {
            if(message.HasValue())
                return Json(message, JsonRequestBehavior.AllowGet);
            return Json(true, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 判断验证的结果。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <param name="args">格式化参数。</param>
        /// <returns>返回Json试图结果。</returns>
        protected virtual JsonResult JsonLocalCheckResult(object key, params object[] args)
        {
            return Json(Local(key, args), JsonRequestBehavior.AllowGet);
        }
    }
}
